package com.captain.leetcode;

import java.util.Objects;

/**
 * @author captain
 * @Des //输入：strs = ["flower","flow","flight"]
 * //输出："fl"
 * //
 * //
 * // 示例 2：
 * //
 * //
 * //输入：strs = ["dog","racecar","car"]
 * //输出：""
 * //解释：输入不存在公共前缀。
 * @Date 2021/7/14 23:11
 */
public class 最长公共前缀14 {
    public static void main(String[] args) {
        String[] arr = {"flower", "flow", "lo"};
        System.out.println(longestCommonPrefix(arr));
    }

    public static String longestCommonPrefix(String[] strs) {
        if (Objects.isNull(strs) || strs.length == 0) return "";
        String prefix = strs[0];
        for (String s : strs) {
            while (s.indexOf(prefix) != 0) {
                prefix = prefix.substring(0, prefix.length() - 1);
            }
        }
        return prefix;
    }
}
